package com.yanqu.road.dao.impl.channelcenter.season;

import com.yanqu.road.dao.cache.SqlCacheParam;
import com.yanqu.road.dao.impl.TempNoPoolCacheDao;
import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.entity.season.SeasonRewardConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TempSeasonRewardConfigDaoImpl extends TempNoPoolCacheDao {

    public TempSeasonRewardConfigDaoImpl(String dbParam) {
        super(dbParam);
    }

    public Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>> getSeasonRewardConfigMap(int activityId, int seasonId) {

        String sql = "select * from `t_n_washepvp_reward` where `activityId`=? and ( `seasonId`=? or `seasonId`=? )";
        SqlCacheParam sqlCacheParam = getCacheParam(sql);
        sqlCacheParam.addParam(Integer.class, activityId);
        sqlCacheParam.addParam(Integer.class, WasheConfig.SEASON_ID_NULL);
        sqlCacheParam.addParam(Integer.class, seasonId);
        // 先读缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>>) cacheResult;
        }

        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, Map<Integer, SeasonRewardConfig>>> mapMapMap = new ConcurrentHashMap<>();
        try {
            call = createCall(sqlCacheParam);
            rs = call.executeQuery();
            while (rs.next()) {
                SeasonRewardConfig seasonRewardConfig = parseRewardConfig(rs);

                // put
                mapMapMap.putIfAbsent(seasonRewardConfig.getSeasonId(), new ConcurrentHashMap<>());
                mapMapMap.get(seasonRewardConfig.getSeasonId()).putIfAbsent(seasonRewardConfig.getType(), new ConcurrentHashMap<>());
                mapMapMap.get(seasonRewardConfig.getSeasonId()).get(seasonRewardConfig.getType()).put(seasonRewardConfig.getRewardId(), seasonRewardConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection(call, rs);
        }
        // 添加缓存
        addCacheResult(sqlCacheParam, mapMapMap);
        return mapMapMap;
    }

    public long getWasheNewHandScore(int activityId) {

        String sql = "select max(`param`+0) as newHandScore from `t_n_washepvp_reward` where `activityId`=? and `seasonId`=? and `type`=?";
        SqlCacheParam sqlCacheParam = getCacheParam(sql);
        sqlCacheParam.addParam(Integer.class, activityId);
        sqlCacheParam.addParam(Integer.class, WasheConfig.SEASON_ID_NULL);
        sqlCacheParam.addParam(Integer.class, eSeasonRewardType.WasheNewHandScoreArch.getIntValue());
        // 先读缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (long) cacheResult;
        }

        long newHandScore = Long.MAX_VALUE;
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = createCall(sqlCacheParam);
            rs = call.executeQuery();
            if (rs.next()) {
                newHandScore = rs.getLong("newHandScore");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection(call, rs);
        }
        // 添加缓存
        addCacheResult(sqlCacheParam, newHandScore);
        return newHandScore;
    }

    public Map<Integer, SeasonRewardConfig> getWasheZhanLingConfigMap(int activityId, int seasonId) {
        String sql = "select * from `t_n_washepvp_reward` where `activityId`=? and `seasonId`=? and `type`=?";
        SqlCacheParam sqlCacheParam = getCacheParam(sql);
        sqlCacheParam.addParam(Integer.class, activityId);
        sqlCacheParam.addParam(Integer.class, seasonId);
        sqlCacheParam.addParam(Integer.class, eSeasonRewardType.WasheGetScoreZhanLing.getIntValue());
        // 先读缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (Map<Integer, SeasonRewardConfig>) cacheResult;
        }
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, SeasonRewardConfig> map = new ConcurrentHashMap<>();
        try {
            call = createCall(sqlCacheParam);
            rs = call.executeQuery();
            while (rs.next()) {
                SeasonRewardConfig seasonRewardConfig = parseRewardConfig(rs);

                // put
                map.putIfAbsent(seasonRewardConfig.getRewardId(), seasonRewardConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection(call, rs);
        }
        // 添加缓存
        addCacheResult(sqlCacheParam, map);
        return map;
    }

    private SeasonRewardConfig parseRewardConfig(ResultSet rs) throws SQLException {
        SeasonRewardConfig seasonRewardConfig = new SeasonRewardConfig();
        seasonRewardConfig.setSeasonId(rs.getInt("seasonId"));
        seasonRewardConfig.setRewardId(rs.getInt("rewardId"));
        seasonRewardConfig.setTitle(rs.getString("title"));
        seasonRewardConfig.setDesc(rs.getString("desc"));
        seasonRewardConfig.setType(rs.getInt("type"));
        seasonRewardConfig.setParam(rs.getString("param"));
        seasonRewardConfig.setReward(rs.getString("reward"));

        // init
        seasonRewardConfig.setParamList(StringUtils.stringToLongList(seasonRewardConfig.getParam(), "\\|"));
        return seasonRewardConfig;
    }

}
